package com.graduation_design.modules.app.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置交换机与队列的相关信息
 */
@Configuration
public class RabbitConfig {

    public final static String ORDER_CANCEL_EXCHANGE = "order-cancel-exchange";
    public final static String ORDER_CANCEL_QUEUE = "order-cancel-queue";
    public final static String ORDER_CANCEL_ROUTE_KEY = "order-cancel-route-key";

    //订单消息实际消费队列所绑定的交换机
    @Bean
    public DirectExchange orderCancelDirectExchange() {
        return new DirectExchange(ORDER_CANCEL_EXCHANGE, true, false);
    }

    //订单延迟队列（死信队列）
    @Bean
    public Queue orderCancelQueue() {
        return QueueBuilder.durable(ORDER_CANCEL_QUEUE)
                // 绑定死信队列
                .deadLetterExchange(ORDER_CANCEL_TTL_EXCHANGE)
                .deadLetterRoutingKey(ORDER_CANCEL_TTL_ROUTE_KEY)
                .build();
    }

    @Bean
    Binding orderBinding(){
        return BindingBuilder
                .bind(orderCancelQueue())
                .to(orderCancelDirectExchange())
                .with(ORDER_CANCEL_ROUTE_KEY);
    }

    // 死信队列
    public final static String ORDER_CANCEL_TTL_EXCHANGE = "order-cancel-ttl-exchange";
    public final static String ORDER_CANCEL_TTL_QUEUE = "order-cancel-ttl-queue";
    public final static String ORDER_CANCEL_TTL_ROUTE_KEY = "order-cancel-ttl-route-key";

    //订单消息实际消费队列所绑定的交换机
    @Bean
    public DirectExchange orderCancelTtlDirectExchange() {
        return new DirectExchange(ORDER_CANCEL_TTL_EXCHANGE, true, false);
    }

    //订单延迟队列（死信队列）
    @Bean
    public Queue orderCancelTtlQueue() {
        return QueueBuilder.durable(ORDER_CANCEL_TTL_QUEUE)
                .build();
    }

    @Bean
    Binding orderCancelTtlBinding(){
        return BindingBuilder
                .bind(orderCancelTtlQueue())
                .to(orderCancelTtlDirectExchange())
                .with(ORDER_CANCEL_TTL_ROUTE_KEY);
    }

}
